package com.yuan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yuan.entity.dao.ChatSession;
import com.yuan.mapper.ChatSessionMapper;
import com.yuan.service.ChatSessionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class ChatSessionServiceImpl implements ChatSessionService {

    private final ChatSessionMapper chatSessionMapper;

    @Override
    public ChatSession queryById(Long id) {
        return chatSessionMapper.selectById(id);
    }



    private LambdaQueryWrapper<ChatSession> buildQueryWrapper(ChatSession bo) {
        LambdaQueryWrapper<ChatSession> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ChatSession::getCreateTime);
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceId()) , ChatSession::getDeviceId, bo.getDeviceId());
        lqw.eq(StringUtils.isNotBlank(bo.getSessionId()), ChatSession::getSessionId, bo.getSessionId());
        return lqw;
    }

    @Override
    public List<ChatSession> queryList(ChatSession entity) {
        LambdaQueryWrapper<ChatSession> lqw = buildQueryWrapper(entity);
        return chatSessionMapper.selectList(lqw);
    }

    @Override
    public Boolean insertByEntity(ChatSession entity) {
        return chatSessionMapper.insert(entity)>0;
    }

    @Override
    public Boolean updateByEntity(ChatSession entity) {
        return chatSessionMapper.updateById(entity)>0;
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return chatSessionMapper.deleteByIds(ids) > 0;
    }


    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ChatSession entity){
        //TODO 做一些数据校验,如唯一约束
    }

}
